import api from '@/utils/api'
import {
  // getToken,
  setToken,
  setRefToken,
  getToken,
  removeToken
} from '@/utils/auth'
import {resetRouter} from '@/router'
import Cookies from 'js-cookie'
export default {
  namespaced: true,
  state: {
    id: 0,
    name: '',
    userInfo: {},
    token: '',
    menuList: [],
    permissionList: [],
    currentMenuId: '',
    appList: [],
    defaultSendInfo: {}
  },
  mutations: {
    SET_USER_INFO (state, userInfo) {
      state.userInfo = userInfo
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_MENULIST: (state, list) => {
      state.menuList = list
    },
    updateName (state, name) {
      state.name = name
    },
    SET_PERMISSIONLIST: (state, list) => {
      state.permissionList = list
    },
    SET_CURRENT_MENU: (state, menuId) => {
      state.currentMenuId = menuId
      Cookies.set('currentMenuId', menuId)
    },
    SET_APP_LIST: (state, appList) => {
      state.appList = appList
    },
    SET_SEND_INFO: (state, info) => {
      state.defaultSendInfo = info
    }
  },
  actions: {
    // user login
    login ({commit}, userInfo) {
      const {username, password} = userInfo
      return new Promise((resolve, reject) => {
        api.login({
          username: username.trim(),
          password: password
        }).then(response => {
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    //  获取token
    getLoginToken ({commit, state}, params) {
      return new Promise((resolve, reject) => {
        api.getLoginToken(params).then(response => {
          const {access_token = '', refresh_token = ''} = response.data
          // console.log('cm', access_token)
          commit('SET_TOKEN', access_token)
          setToken(access_token)
          setRefToken(refresh_token)
          // getMenu
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    //  获取token
    loginInApp ({commit, state}, params) {
      return new Promise((resolve, reject) => {
        api.loginInApp(params).then(response => {
          const {
            access_token = '',
            refresh_token = ''
          } = response.data
          // console.log('cm', access_token)
          commit('SET_TOKEN', access_token)
          setToken(access_token)
          setRefToken(refresh_token)
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取菜单
    getMenu ({commit, state}, params) {
      return new Promise((resolve, reject) => {
        api.getMenu().then(response => {
          let menuList = response.data.menuList
          commit('SET_MENULIST', menuList)
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    getUserInfo ({commit, state}, params) {
      return new Promise((resolve, reject) => {
        api.getUserInfo().then(response => {
          if (!response.data) {
            resolve(false)
          }
          commit('SET_USER_INFO', response.data)
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // remove token
    resetToken ({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    setAppList ({commit}, appList) {
      commit('SET_APP_LIST', appList)
    }
  }
}
